package com.lenovo.smarttraffic.ui.activity.stage4;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckedTextView;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.BusLineOrderBean;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;

import java.util.List;

public class MyOrderActivity extends BaseActivity {

    private CheckedTextView tv1, tv2;
    private ExpandableListView listView;

    private Drawable ripple;

    @Override
    protected int getLayout() {
        return R.layout.activity_my_order;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initToolBar(this, "我的订单");

        tv1 = findViewById(R.id.tv1);
        tv2 = findViewById(R.id.tv2);
        listView = findViewById(R.id.ex_listView);

        ripple = tv2.getBackground();

        tv1.setOnClickListener(v -> {
            if (!tv1.isChecked()) {
                tv1.setChecked(true);
                tv2.setChecked(false);
                tv1.setBackground(getDrawable(R.drawable.tab_indicate));
                tv2.setBackground(ripple);
                listView.setAdapter(new MyOrderAdapter(0));
            }
        });

        tv2.setOnClickListener(v -> {
            if (!tv2.isChecked()) {
                tv1.setChecked(false);
                tv2.setChecked(true);
                tv1.setBackground(ripple);
                tv2.setBackground(getDrawable(R.drawable.tab_indicate));
                listView.setAdapter(new MyOrderAdapter(1));
            }
        });

        tv1.performClick();
    }
}

class MyOrderAdapter extends BaseExpandableListAdapter {

    public static List<BusLineOrderBean.ROWSDETAILBean> list = CustomBusActivity.orderBeanList;

    private int type;

    public MyOrderAdapter(int type) {
        this.type = type;
    }

    @Override
    public int getGroupCount() {
        int count = 0;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getFlag() == type) {
                count++;
            }
        }
        return count;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        //分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们
        return true;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder viewHolder;

        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.exlist_order_group, parent, false);
            viewHolder = new GroupViewHolder();
            viewHolder.info_tv = convertView.findViewById(R.id.info_tv);
            viewHolder.id_tv = convertView.findViewById(R.id.id_tv);
            viewHolder.expand = convertView.findViewById(R.id.expand);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (GroupViewHolder) convertView.getTag();
        }

        BusLineOrderBean.ROWSDETAILBean bean = list.get(groupPosition);
        if (bean.getFlag() == type) {
            viewHolder.info_tv.setText(bean.getBusstop_start() + "————" + bean.getBusstop_end() + "\n" + "票价：￥2.0");
            viewHolder.id_tv.setText("订单编号" + bean.getId());
        }

        //如果是展开状态，
        if (isExpanded) {
            viewHolder.expand.setRotation(270);
        } else {
            viewHolder.expand.setRotation(180);
        }

        return convertView;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder viewHolder;

        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.exlist_order_child, parent, false);
            viewHolder = new ChildViewHolder();
            viewHolder.date_tv = convertView.findViewById(R.id.date_tv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ChildViewHolder) convertView.getTag();
        }

        BusLineOrderBean.ROWSDETAILBean bean = list.get(groupPosition);
        if (bean.getFlag() == type) {
            viewHolder.date_tv.setText(bean.getBusstop_date().replace(", ", "\n").trim());
        }

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        //指定位置上的子元素是否可选中
        return true;
    }

    static class GroupViewHolder {
        TextView info_tv, id_tv;
        View expand;
    }

    static class ChildViewHolder {
        TextView date_tv;
    }
}
